package ${serviceImplPackage};
#parse("/template/func.vm") #set ($className =  $table.className)
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.ui.ModelMap;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;

import ${comonPath}.common.components.DataGrid;
import ${comonPath}.common.components.DataGridJson;
import ${comonPath}.common.entity.data.IData;
import ${comonPath}.common.utilself.CommonUtil;
import ${comonPath}.common.utilself.Utility;

import ${entityPackage}.${className};
import ${daoPackage}.${className}Dao;
import ${servicePackage}.${className}Service;
import ${page};
import ${criteria};

/**
 * @ClassName: ${className}Service
 * @Description: $!table.remark
 * @author: $author
 * @version:v1.0.0
 * @date: $now
 * @Copyright $copyright
 */
@Service//(value = "#uncap_first(${className})Service")
public class ${className}ServiceImpl implements ${className}Service {
	
	private static final Logger logger = LoggerFactory.getLogger(${className}ServiceImpl.class);
    @Resource
    private ${className}Dao #uncap_first(${className})Dao;

    /**
     * @param #uncap_first(${className}) ${className}参数
     * @return int 结果个数
     * @Title #uncap_first(${className})Service.count
     * @Description: 根据查询条件，计算${className}个数   ($table.tableName)
     */
    public int count(${className} #uncap_first(${className})) {
        Criteria example = Criteria.createCriteria();
        Criteria.Condition condition = example.createConditon();
#STMT_EXAMPLE
        return #uncap_first(${className})Dao.countByExample(example);
    }

    /**
     * @param #uncap_first(${className}) ${className}参数
     * @return void
     * @Title #uncap_first(${className})Service.save
     * @Description: 保存  ${className}, 【全字段】
     */
    @Override
    public int save(${className} #uncap_first(${className})) {
        return #uncap_first(${className})Dao.insert(#uncap_first(${className}));
    }

    /**
     * @param #uncap_first(${className}) ${className}参数
     * @return void
     * @Title #uncap_first(${className})Service.saveSelective
     * @Description: 保存 ${className}, 只插入${className}的【非空】字段
     */
    public int saveSelective(${className} #uncap_first(${className})) {
        return this.#uncap_first(${className})Dao.insertSelective(#uncap_first(${className}));
    }

    /**
     * @param #uncap_first(${className}) ${className}参数，查询条件
     * @return void
     * @Title #uncap_first(${className})Service.delete
     * @Description: 根据查询条件，删除 ${className}

     */
    public int delete(${className} #uncap_first(${className})) {
        Criteria example = Criteria.createCriteria();
        Criteria.Condition condition = example.createConditon();
#STMT_EXAMPLE
        return this.#uncap_first(${className})Dao.deleteByExample(example);
    }

    /**
     * @param #ID_NAME 主键
     * @return void
     * @Title #uncap_first(${className})Service.deleteByPrimaryKey
     * @Description: 根据主键，删除 ${className}
     */
    @Override
    public int deleteByPrimaryKey(#ID_TYPE_NAME) {
        return #uncap_first(${className})Dao.deleteByPrimaryKey(#ID_NAME);
    }

    /**
     * @param update    ${className}参数需改的值
     * @param #uncap_first(${className}) ${className}查询条件
     * @return void
     * @Title #uncap_first(${className})Service.update
     * @Description: 修改${className}, 根据查询条件修改【全字段】
     */
    @Override
    public int update(${className} update, ${className}  #uncap_first(${className}) ) {
        Criteria example = Criteria.createCriteria();
        Criteria.Condition condition = example.createConditon();
#STMT_EXAMPLE
        return #uncap_first(${className})Dao.updateByExample(update, example);
    }
    
    /**
     * @param #uncap_first(${className}) ${className}参数
     * @return void
     * @Title #uncap_first(${className})Service.updateByPrimaryKey
     * @Description: 修改${className}, 根据主键，修改【全字段】
     */
    @Override
    public int updateByPrimaryKey(${className} #uncap_first(${className})) {
        return #uncap_first(${className})Dao.updateByPrimaryKey(#uncap_first(${className}));
    }

    /**
     * @param #uncap_first(${className}) ${className}参数
     * @return void
     * @Title #uncap_first(${className})Service.updateSelectiveByPrimaryKey
     * @Description: 修改${className}, 根据主键，只修改【非空】参数字段
     */
    @Override
    public int updateSelectiveByPrimaryKey(${className} #uncap_first(${className})) {
        return #uncap_first(${className})Dao.updateSelectiveByPrimaryKey(#uncap_first(${className}));
    }

    /**
     * @param update    ${className}参数需改的值
     * @param #uncap_first(${className}) ${className}查询条件
     * @return void
     * @Title #uncap_first(${className})Service.updateSelective
     * @Description: 修改${className}, 根据查询条件修改【非空】参数字段
     */
    @Override
    public int updateSelective(${className} update, ${className}  #uncap_first(${className}) ) {
        Criteria example = Criteria.createCriteria();
        Criteria.Condition condition = example.createConditon();
#STMT_EXAMPLE
        return #uncap_first(${className})Dao.updateSelectiveByExample(update, example);
    }
    
    /**
     * @param #ID_NAME 主键
     * @return ${className}参数
     * @Title #uncap_first(${className})Service.selectByPrimaryKey
     * @Description: 查询返回${className}，根据【主键】
     */
    @Override
    public ${className} selectByPrimaryKey(#ID_TYPE_NAME) {
        return #uncap_first(${className})Dao.selectByPrimaryKey(#ID_NAME);
    }
    
    /**
     * @param #uncap_first(${className}) ${className}参数，查询条件
     * @return List<${className}> ${className}结果集 [不分页]
     * @Title #uncap_first(${className})Service.selectAll
     * @Description: 查询返回List<${className}>结果集，根据查询条件   ($table.tableName) 
     */
    @Override
    public List<${className}> selectAll(${className} #uncap_first(${className})) {
        Criteria example = Criteria.createCriteria();
        Criteria.Condition condition = example.createConditon();
#STMT_EXAMPLE_NOTNULL

        return #uncap_first(${className})Dao.selectByExample(example);
    }

    /**
     * @param #uncap_first(${className}) ${className}参数，查询条件
     * @param page Page参数，分页条件
     * @return Page<${className}> ${className}结果集
     * @Title #uncap_first(${className})Service.select
     * 
     * @Description: 根据查询条件类，返回Page<${className}>结果集 [分页]  ($table.tableName) 
     * 
     * 分页拦截器代码会对sql处理计算出总数  page.getCount();
     * 演示 Page 类型返回  DataGridJson 参见  selectByPageJson方法
     */
    @Override
    public Page<${className}> selectByPage(${className} #uncap_first(${className}), Page page) {
        Criteria example = Criteria.createCriteria();
        Criteria.Condition condition = example.createConditon();
#STMT_EXAMPLE_NOTNULL

        page.setList(#uncap_first(${className})Dao.selectByExample(example));
        return page;
    }
    
    /**
     * @param  #uncap_first(${className}) ${className}参数，查询条件
     * @param  DataGrid DataGrid参数，分页条件
     * @return DataGridJson<${className}> ${className}结果集
     * @Title  #uncap_first(${className})Service.selectByPageJson
     * 
     * @Description: 根据查询条件类，返回JSON格式结果集 [分页]  ($table.tableName) 
     * 
     * Page   DataGrid  ModelMap  重复了，看个人习惯使用
     * 排序使用             example.setOrderByClause(" LOGIN_NAME desc ");
     * 分页可以写        example.setPage(page) 
     * 或者自己拼写   RowBounds rowBounds = new RowBounds(page.getFirstResult(),page.getPageSize());
     * 演示 DataGridJson 类型返回 Page 参见 selectByPage 方法
     */
    @Override
    public DataGridJson selectByPageJson(HttpServletRequest request ,${className} #uncap_first(${className}),Page page ,DataGrid dg ,ModelMap mMap){
      	Criteria example = Criteria.createCriteria();
    	Criteria.Condition condition = example.createConditon();
#STMT_EXAMPLE_NOTNULL

		//分页
		example.setPage(page);
    	List<${className}> list = #uncap_first(${className})Dao.selectByExample(example);
    	
		DataGridJson json = new DataGridJson();
		json.setRows(list);
		json.setTotal(page.getCount());//拦截器自己计算总数
		return json; 
    }
    
	/**
	 * @param  Map 参数，查询条件
	 * @param  DataGrid DataGrid参数，分页条件
	 * @return DataGridJson<${className}> ${className}结果集
	 * @Title  #uncap_first(${className})Service.selectByPageMap
	 * 
	 * @Description: 根据查询条件类，返回Page<Map<String, String>>结果集 [分页]  ($table.tableName)
	 * 
	 * Page  DataGrid  ModelMap  重复了，看个人习惯使用
	 * 分页  Criteria example = Criteria.createCriteria();
	 * example.setOrderByClause(" LOGIN_NAME desc ");
	 * inparam.put("doYouCanDo", example);
	 */
	@Override
	public Page<Map<String, String>> selectByPageMap(HttpServletRequest request, Page<Map<String, String>> page ,DataGrid dg ,ModelMap mMap){
		//Map<String, Object> inparam = new HashMap<String, Object>();
		//Map<String, Object> inparam =  Utility.getParamAsMap(request);
		IData<String, String> inparam = Utility.getParamAsDataMap(request);
		RowBounds rowBounds = new RowBounds(dg.getFirstResult(),dg.getPageSize());	//you can use dg or page ,do you like 
		//如果不是分页 可以手工设置为  RowBounds rowBounds = new RowBounds(0,99999);基本也够用了
		List<Map<String, String>> list = #uncap_first(${className})Dao.queryInfoByMap(inparam, rowBounds);
		int roleCount =  #uncap_first(${className})Dao.countInfoByMap(inparam); //统计类使用自己的统计方法，效率也高
		//DataGridJson json = new DataGridJson();
		page.setList(list);
		page.setCount((long)roleCount);
		return page; 
	}



}
#* ===================函数定义=================== *###
#*  设置example方法  *###
#macro ( STMT_EXAMPLE ) #foreach ( $column in $table.columns ) ###
       if (#uncap_first(${className}).get#cap_first(${column.fieldName})() != null) {condition.andEqualTo("${column.columnName}", #uncap_first(${className}).get#cap_first(${column.fieldName})());}
#end #end

#macro ( STMT_EXAMPLE_NOTNULL ) #foreach ( $column in $table.columns ) ###
	    if (!CommonUtil.isNull(#uncap_first(${className}).get#cap_first(${column.fieldName})())) {condition.andEqualTo("${column.columnName}", #uncap_first(${className}).get#cap_first(${column.fieldName})());}
#end #end

